﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ashlen.Portal.Core.DataInterfaces;
using Ashlen.Portal.Core;

namespace Ashlen.Portal.Data.SqlServer
{
    public class SecurityDao : BaseDao, ISecurityDao
    {
        public SecurityDao() : base()
        {}

        public SecurityDao(string connectionString)
            : base(connectionString)
        {}

        public object GetEmailPref(Guid userid)
        {
            RenewContext();
            return Context.aspnet_UserEmailPreference.FirstOrDefault(p => p.UserId == userid);
        }

        public object GetPersonalInfo(Guid userid)
        {
            RenewContext();
            var query = from q in Context.aspnet_UserPersonalInfo
                        where q.UserId == userid
                        select q;
            return query.FirstOrDefault();
        }

        public void SaveAvatar(Guid userId, byte[] avatar)
        {
            RenewContext();
            var main = Context.aspnet_Users.Include("aspnet_UserPersonalInfo").Include("aspnet_UserEmailPreference").FirstOrDefault(u => u.UserId == userId);
            if (main.aspnet_UserPersonalInfo != null)
            {
                main.aspnet_UserPersonalInfo.Avatar = avatar;
                main.aspnet_UserPersonalInfo.DateUpdated = DateTime.Now;

                Context.SaveChanges();
            }
        }

        public void CreatePersonalInfo(Guid userId, string firstName, string lastName, string nickName, 
            DateTime? dateOfBirth, string sex, string mobile, string phone, string country, string state, int? postcode, string comment,
            bool acceptCommunityEmail, bool acceptAdvertisingEmail)
        {
            RenewContext();
            var main = Context.aspnet_Users.Include("aspnet_UserPersonalInfo").Include("aspnet_UserEmailPreference").FirstOrDefault(u => u.UserId == userId);
            // Check to see if the user exists
            if (main.aspnet_UserPersonalInfo == null)
            {
                main.aspnet_UserPersonalInfo = new aspnet_UserPersonalInfo { UserId = userId, DateCreated = DateTime.Now, DateUpdated=DateTime.Now, Firstname = firstName,
                Lastname = lastName, Nickname = nickName, DateOfBirth = dateOfBirth, Sex=sex, Mobile=mobile, Phone=phone, Country=country,
                State=state, Postcode= postcode, Comment =comment};
            }
            else
            {
                main.aspnet_UserPersonalInfo.DateCreated = DateTime.Now;
                main.aspnet_UserPersonalInfo.DateUpdated = DateTime.Now;
                main.aspnet_UserPersonalInfo.Firstname = firstName;
                main.aspnet_UserPersonalInfo.Lastname = lastName; 
                main.aspnet_UserPersonalInfo.Nickname = nickName;
                main.aspnet_UserPersonalInfo.DateOfBirth = dateOfBirth;
                main.aspnet_UserPersonalInfo.Sex = sex;
                main.aspnet_UserPersonalInfo.Mobile = mobile;
                main.aspnet_UserPersonalInfo.Phone = phone;
                main.aspnet_UserPersonalInfo.Country = country;
                main.aspnet_UserPersonalInfo.State = state;
                main.aspnet_UserPersonalInfo.Postcode = postcode;
                main.aspnet_UserPersonalInfo.Comment = comment;               
            }
            if (main.aspnet_UserEmailPreference == null)
            {
                main.aspnet_UserEmailPreference = new aspnet_UserEmailPreference { AcceptCommunityEmail = acceptCommunityEmail, 
                    AcceptAdvertisingEmail = acceptAdvertisingEmail, DateCreated = DateTime.Now, DateUpdated=DateTime.Now };
            }
            else
            {
                main.aspnet_UserEmailPreference.DateUpdated = DateTime.Now;
                main.aspnet_UserEmailPreference.AcceptAdvertisingEmail = acceptAdvertisingEmail;
                main.aspnet_UserEmailPreference.AcceptCommunityEmail = acceptCommunityEmail;
            }

            main.LastActivityDate = DateTime.Now;
            main.aspnet_UserPersonalInfo.DateUpdated = DateTime.Now;

            Context.SaveChanges();
        }

    }
}
